/**
 * 
 */
package com.ctg.payroll2.util.xlsImport;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import jxl.Workbook;
import jxl.write.Boolean;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.DateTime;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import com.ctg.payroll2.bean.common.ColumnModel;
/**
 * @author 郑致力
 *
 */
public final class JxlExport {
	@SuppressWarnings("unchecked")
	public static void writeExcel(OutputStream out, List data, List<ColumnModel> titles) throws WriteException, IntrospectionException, IOException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		int rowNum = 0;
		int colNum = 0;
		List<String> properties = null;
		WritableCell writableCell = null;
		PropertyDescriptor propertyDescriptor = null;
		Object value = null;
		
		if (data == null || titles == null) {
			throw new java.lang.NullPointerException("传入的参数为NULL值!");
		}
		
		if (!titles.isEmpty()) {
			properties = new ArrayList<String>(titles.size());
		} else {
			return;
		}
		
		WritableWorkbook workbook = Workbook.createWorkbook(out);
		WritableSheet ws = workbook.createSheet("sheet 1", 0);
		//写列名，并设置property队列
		for (ColumnModel cm : titles) {
			writableCell = new Label(colNum++, rowNum, cm.getHeader());
			properties.add(cm.getDataIndex());
			ws.addCell(writableCell);
		}
		
		for (Object o : data) {
			colNum = 0;
			rowNum++;
			for (String property : properties) {
				propertyDescriptor = new PropertyDescriptor(property,o.getClass());
				value = propertyDescriptor.getReadMethod().invoke(o, null);
				writableCell = getWritableCell(colNum++, rowNum, value);
				if ( null != writableCell) {
					ws.addCell(writableCell);
				}
			}
		}
		workbook.write();
        workbook.close();
	}
	
	private static WritableCell getWritableCell(final int colIndex, final int rowIndex, Object value) {
		if (null == value) {
			return null;
		}
		/**
		if (value instanceof java.lang.Short) {
			return new Number(colIndex, rowIndex, ((Short) value).intValue(), null);
		} else if (value instanceof java.lang.Integer) {
			return new Number(colIndex, rowIndex, ((Integer) value).intValue(), null);
		} else if (value instanceof java.lang.Long) {
			return new Number(colIndex, rowIndex, ((Long) value).longValue(), null);
		} else if (value instanceof java.lang.Float) {
			return new Number(colIndex, rowIndex, ((Float) value).floatValue(), null);
		} else if (value instanceof java.lang.Double) {
			return new Number(colIndex, rowIndex, ((Double) value).doubleValue(), null);
		} else if (value instanceof java.lang.Boolean) {
			return new Boolean(colIndex, rowIndex, ((Boolean) value).isHidden());
		} else if (value instanceof java.util.Date) {
			return new DateTime(colIndex, rowIndex, (java.util.Date) value);
		}**/
		
		return new Label(colIndex, rowIndex, value.toString());
	}
	
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		String ss = "[new Ext.grid.RowNumberer(), " +
				"{header:'',dataIndex:'payId',hidden:true}," +
				"{header:'',dataIndex:'his_id',hidden:true}," +
				"{header:'',dataIndex:'employee_id',hidden:true}," +
				"{header:'员工姓名',dataIndex:'name'},{header:'工资月份',dataIndex:'period'}," +
				"columnModelResponseText," +
				"{header:'工资调整',dataIndex:'',width : 60," +
				"renderer:function(v,p,record){" +
				"return \"<input type='button' class='x-btn' value='结果调整'" +
				" onClick=\"salaryAjust('\" + record.data.payId + \"');\">\"}}]";
		System.out.println(ss);
	}
}


class Demo {
	private String name;
	private int age;
	private String position;
	
	public Demo() {}
	public Demo(String name, int age, String position) {
		this.name = name;
		this.age = age;
		this.position = position;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getPosition() {
		return position;
	}
	public void setPosition(String position) {
		this.position = position;
	}
}
